package com.eedi.framework.notice.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.enums.YesOrNoEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.notice.controller.org.vo.OrgNoticeReadReq;
import com.eedi.framework.notice.controller.org.vo.OrgNoticeReadResp;
import com.eedi.framework.notice.controller.org.vo.OrgNoticeResp;
import com.eedi.framework.notice.dal.mapper.OrgNoticeReadMapper;
import com.eedi.framework.notify.controller.sys.vo.SysNoticePageReq;
import com.eedi.framework.notify.controller.sys.vo.SysNoticeResp;
import com.eedi.framework.notify.service.SysNoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.eedi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.eedi.framework.notice.enums.OrgNoticeErrorCodeConstants.ORG_NOTICE_NOT_EXISTS;

@Service
public class OrgNoticeServiceImpl implements OrgNoticeService {
    @Resource
    private OrgNoticeReadMapper orgNoticeReadMapper;
    @Resource
    private SysNoticeService sysNoticeService;

    @Override
    public PageResult<OrgNoticeResp> page(String orgUserId, SysNoticePageReq pageReqVO) {
        pageReqVO.setSysNoticeStatus(CommonStatusEnum.ENABLE);
        PageResult<SysNoticeResp> noticePage = sysNoticeService.getNoticePage(pageReqVO);
        List<String> sysNoticeIds = noticePage.getList().stream().map(SysNoticeResp::getSysNoticeId).toList();
        Map<String, String> redaStatus = orgNoticeReadMapper.getRedaStatus(sysNoticeIds, orgUserId);
        PageResult<OrgNoticeResp> result = BeanUtils.toBean(noticePage, OrgNoticeResp.class);
        for (int i = 0; i < result.getList().size(); i++) {
            if (StrUtil.isNotBlank(redaStatus.get(result.getList().get(i).getSysNoticeId()))) {
                result.getList().get(i).setRead(YesOrNoEnum.YES);
            } else {
                result.getList().get(i).setRead(YesOrNoEnum.NO);
            }
        }
        return result;
    }

    @Override
    public void readAll(String orgUserId) {
        List<OrgNoticeReadResp> orgNoticeReads = orgNoticeReadMapper.selectReadByOrgUserId(orgUserId);
        List<String> sysNoticeIds
                = orgNoticeReads.stream().map(OrgNoticeReadResp::getSysNoticeId).toList();
        List<SysNoticeResp> notReadNotice = sysNoticeService.getNoticeNotInIds(sysNoticeIds);
        List<String> list = notReadNotice.stream()
                .filter(x-> CommonStatusEnum.ENABLE.getCode().equals(x.getSysNoticeStatus().getCode()))
                .map(SysNoticeResp::getSysNoticeId).toList();
        List<OrgNoticeReadReq> createReq = new ArrayList<>();
        list.forEach(x -> {
            OrgNoticeReadReq req = new OrgNoticeReadReq();
            req.setOrgUserId(orgUserId);
            req.setSysNoticeId(x);
            createReq.add(req);
        });
        orgNoticeReadMapper.create(createReq);
    }

    @Override
    public void read(String sysNoticeId, String orgUserId) {
        SysNoticeResp notice = sysNoticeService.getNotice(sysNoticeId);
        if (ObjectUtil.isEmpty(notice)) {
            throw exception(ORG_NOTICE_NOT_EXISTS);
        }
        OrgNoticeReadResp readResp = orgNoticeReadMapper.select(sysNoticeId, orgUserId);
        if (ObjectUtil.isEmpty(readResp)) {
            orgNoticeReadMapper.create(orgUserId, sysNoticeId);
        }
    }
}
